#!/usr/bin/env python

from links_class import Links


def list_container ( liwe ):
	"""
	:INPUT:

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	l = Links ( liwe )

	return { 'cnts': l.list_container () }

def create ( liwe, id_cnt, url, testo, sticky, target = 1, id_link = None, active = 1 ):
	"""
	:INPUT:
		- id_cnt:		SAFE_NUMBER
		- id_link:		SAFE_NUMBER
		- sticky:		SAFE_NUMBER
		- testo:		SAFE_DB_STRING
		- url:			SAFE_DB_STRING
		- target:		SAFE_NUMBER
		- active:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	l = Links ( liwe )

	id_link = l.create ( id_cnt, url, testo, sticky, target, id_link, active )

	return { "id_link": id_link }

def search ( liwe, id_cnt = None, _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- id_cnt:		SAFE_NUMBER
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	limit = _X_LINES

	l = Links ( liwe )
	res = l.search ( id_cnt, start_point, limit )

	count = 0
	for v in res [ 'rows' ]:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v

		v [ '_img' ] = ''

		count += 1

	res [ 'rows' ] = res [ 'tot_rows' ]

	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = _X_LINES

	return res

def delete ( liwe, ids ):
	"""
	:INPUT:
		- ids:		SAFE_DB_STRING

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	l = Links ( liwe )

	l.delete ( ids)

	return { "status": "ok" }

def get ( liwe, id_link ):
	"""
	:INPUT:
		- id_link:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	l = Links ( liwe )

	return { "link": l.get ( id_link ) }
